package com.tekj.cxqcb.view.Module.activity.StoreModule;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.squareup.picasso.Picasso;
import com.tekj.cxqcb.R;
import com.tekj.cxqcb.config.enums.EventBusMsgBean;
import com.tekj.cxqcb.operation.resultBean.CarStoresBean;
import com.tekj.cxqcb.view.Module.fragment.ModifyAddress2Fragment;
import com.tekj.cxqcb.view.Module.fragment.ModifyAddressFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import common.base.activity.BaseActivity;
import common.base.adapter.TheFragmentPagerAdapter;
import common.tool.Tools;

import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.提交门店地址;
import static com.tekj.cxqcb.config.enums.EventBusMsgBean.EnumStatus.门店地址图片;

/**
 * 提车门店地址
 */
public class ModifyStoreAddressActivity extends BaseActivity {

    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_l)
    TextView tvL;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_r)
    ImageView ivR;
    @BindView(R.id.tv_r)
    TextView tvR;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.rl_title_bg)
    RelativeLayout rlTitleBg;
    @BindView(R.id.magicIndicator)
    MagicIndicator magicIndicator;
    @BindView(R.id.vp_main)
    ViewPager vpMain;
    @BindView(R.id.btn_modify)
    TextView btnModify;

    private List<Fragment> fragments;
    private String[] titles = {"自己门店", "公用门店"};
    public CarStoresBean.DataBean dataBean;

    @Override
    public void init() {
        dataBean = (CarStoresBean.DataBean) getIntent().getSerializableExtra("data");
        tvTitle.setText("修改地址");
        fragments = new ArrayList<>();
        initIndicator();
    }

    @Override
    public int setLayout() {
        return R.layout.activity_modify_store_address;
    }

    @Override
    public void initLoadStart() {

    }

    @Override
    public void initLoadEnd() {

    }

    @Override
    public void initBindingView(Object mBean) {

    }


    private void newFragemt(int type) {
        Fragment fragment;
        if (type == 0) {
            fragment = new ModifyAddressFragment();
        } else {
            fragment = new ModifyAddress2Fragment();
        }
        Bundle b = new Bundle();
        b.putInt("type", type);
        fragment.setArguments(b);
        fragments.add(fragment);
    }

    private void initIndicator() {
        for (int i = 0; i < titles.length; i++) {
            newFragemt(i);
        }

        CommonNavigator navigator = new CommonNavigator(this);
        navigator.setAdjustMode(true);
        navigator.setEnablePivotScroll(true);
        navigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles == null ? 0 : titles.length;
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int i) {
                SimplePagerTitleView simplePagerTitleView = new SimplePagerTitleView(context);
                simplePagerTitleView.setNormalColor(Color.parseColor("#4d4d4d"));
                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.colorPrimary));
                simplePagerTitleView.setTextSize(15);
                simplePagerTitleView.setText(titles[i]);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        vpMain.setCurrentItem(i);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.colorPrimary));
                indicator.setLineHeight(Tools.dip2Px(activity, 1));
                indicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
                return indicator;
            }
        });
        magicIndicator.setNavigator(navigator);


        ViewPagerHelper.bind(magicIndicator, vpMain);

        TheFragmentPagerAdapter adapter = new TheFragmentPagerAdapter(activity.getSupportFragmentManager(), fragments);
        vpMain.setAdapter(adapter);

        vpMain.setCurrentItem(dataBean==null?0:dataBean.getType() - 1, false);
        vpMain.setOffscreenPageLimit(titles.length);
    }


    @OnClick({R.id.ll_back, R.id.btn_modify})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.btn_modify:
                EventBus.getDefault().post(new EventBusMsgBean(提交门店地址, vpMain.getCurrentItem()));
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 102:
                    LocalMedia imge = PictureSelector.obtainMultipleResult(data).get(0);
                    EventBus.getDefault().post(new EventBusMsgBean(门店地址图片, imge));

                    break;
            }
        }
    }
}
